<template>
  <el-form
    ref="loginFormRef"
    :model="loginForm"
    :rules="loginRules"
    class="login_form"
    label-width="0px"
    label-position="left"
  >
    <h3 class="login_title">系统登录</h3>
    <!-- 用户名 -->
    <el-form-item prop="username">
      <el-input v-model="loginForm.username" prefix-icon="iconfont iconzhanghu"></el-input>
    </el-form-item>
    <!-- 密码 -->
    <el-form-item prop="password">
      <el-input v-model="loginForm.password" prefix-icon="iconfont iconmima" show-password></el-input>
    </el-form-item>
    <!-- 记住密码 -->
    <el-checkbox class="login_remember" v-model="checked" label-position="left">记住密码</el-checkbox>
    <!-- 按钮 -->
    <el-form-item class="bnts">
      <el-button type="primary" @click="login()">登录</el-button>
      <el-button type="info" @click="resetLoginForm()">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      checked: true,
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await this.$http.post("login", this.loginForm);
        // console.log(res);
        if (res.flag == "ok") {
          this.$message.success("登录成功");
          this.$router.push({ path: "/home" }); //页面跳转路由
          window.sessionStorage.setItem("user",res.user); //存储User对象
          window.sessionStorage.setItem("username", res.user.username); //存储username对象
          window.sessionStorage.setItem("userid", res.user.id); //存储userid对象
        } else {
          this.$message.error("验证失败");
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 5px;
    box-shadow: 0 0 2px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #eee;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.bnts {
  display: flex;
  justify-content: flex-end;
}
.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.login_form {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}
.login_remember {
  margin: 0px 0px 35px 0px;
  text-align: left;
}
</style>